package cn.gtv.sdk.dcas.api;

import android.content.Context;
import android.net.Uri;

import com.sad.jetpack.v1.datamodel.api.IDataModelChainInput;
import com.sad.jetpack.v1.datamodel.api.IDataModelInterceptorInput;
import com.sad.jetpack.v1.datamodel.api.IDataModelRequest;
import com.sad.jetpack.v1.datamodel.api.utils.LogcatUtils;

import org.json.JSONObject;

@Deprecated
public class SubmitTrackerSingleDataProtocolInputInterceptorV2 implements IDataModelInterceptorInput<String> {
    private Context context;
    private String serverToken;
    public SubmitTrackerSingleDataProtocolInputInterceptorV2(Context context, String serverToken) {
        this.context = context;
        this.serverToken=serverToken;
    }

    @Override
    public void onInterceptedInput(IDataModelChainInput<String> chainInput) throws Exception {
        IDataModelRequest request=chainInput.request();
        IDataModelRequest.Method method=request.method();
        IDataModelRequest.Creator requestCreator=request.toCreator();
        String body=request.body();
        String url= request.url();
        requestCreator.addHeader("Authorization","DJ "+ serverToken);
        JSONObject json=new JSONObject(body);
        String path=Uri.parse(url).getPath();
        String ts=(System.currentTimeMillis()/1000)+"";
        JSONObject json_event=json.optJSONObject("event");
        String event_type=json_event.optString("type");
        String event_target=json_event.optString("target");
        String product=json.optJSONObject("info").optString("product");
        String data=product+event_type+event_target;
        String signature=SignatureV2Util.createSignatureV2(path,data,ts);
        //开始封装实际提交的json
        JSONObject newJson=new JSONObject();
        newJson.put("eventData",json);
        newJson.put("signature",signature);
        newJson.put("ts",ts);
        LogcatUtils.e("------------->埋点实际提交数据："+newJson.toString(4));
        requestCreator.body(newJson.toString());
        request=requestCreator.create();
        chainInput.proceed(request,null,chainInput.currIndex());
    }




}
